package api

import (
	"gateway/basic/config"
	__ "gateway/basic/proto"
	"gateway/consts"
	"gateway/handler/request"
	pkg "gateway/pkg"
	"time"

	"github.com/gin-gonic/gin"

	"net/http"
)

func Login(c *gin.Context) {
	var param request.Login
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	r, err := config.UserClien.GetUsernameByUsername(c, &__.GetUsernameByUsernameReq{
		Username: param.Username,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	if r.UserId == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	if r.Password != param.Password {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	token := pkg.AuthJwt(uint(r.UserId), time.Hour*2, consts.JWT_Auth)

	c.JSON(http.StatusBadRequest, gin.H{
		"code": 400,
		"mes":  "登录成功",
		"data": token,
	})
}

func CouponCreate(c *gin.Context) {
	var param request.CouponCreate
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	_, err := config.UserClien.CouponCreate(c, &__.CouponCreateReq{
		Title:       param.Title,
		TypeId:      param.TypeId,
		Comment:     param.Comment,
		ClaimStatus: param.ClaimStatus,
		Remaining:   param.Remaining,
		Quantity:    param.Quantity,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{
		"code": 400,
		"mes":  "优惠卷发布成功",
	})
}

func CouponList(c *gin.Context) {
	var param request.CouponList
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	r, err := config.UserClien.CouponList(c, &__.CouponListReq{
		Page:     param.Page,
		PageSize: param.PageSize,
		Title:    param.Title,
		TypeId:   param.TypeId,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{
		"code": 400,
		"mes":  "优惠卷展示成功",
		"data": r.List,
	})
}

func CouponUpdate(c *gin.Context) {
	var param request.CouponUpdate
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	_, err := config.UserClien.CouponUpdate(c, &__.CouponUpdateReq{
		Id:     param.Id,
		Status: param.Status,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{
		"code": 200,
		"mes":  "优惠卷下架成功",
	})
}

func CouponDel(c *gin.Context) {
	var param request.CouponDel
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	_, err := config.UserClien.CouponDel(c, &__.CouponDelReq{
		Id: param.Id,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{
		"code": 400,
		"mes":  "优惠卷删除成功",
	})
}
